import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:hippo_app/models/setting.dart';
import 'package:hippo_app/values/color_value.dart';
import 'package:hippo_app/values/icon_value.dart';
import 'package:hippo_app/values/size_value.dart';
import 'package:hippo_app/values/style_value.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:hippo_app/widgets/app_dialog/app_dialog.dart';
import 'package:hippo_app/widgets/app_header/app_header.dart';
import 'package:hippo_app/widgets/app_scaffold/app_scaffold.dart';
import 'package:hippo_app/widgets/ripple_container/ripple_container.dart';

class SettingPage extends StatefulWidget {
  const SettingPage({super.key});

  @override
  State<SettingPage> createState() => _SettingPageState();
}

class _SettingPageState extends State<SettingPage> {
  final ScrollController scrollController = ScrollController();
  List<Setting> itemList = [
    Setting(
      key: 'gardenName',
      icon: IconValue.listItemGarden,
      color: ColorValue.appPrimaryColor,
      page: ''
    ),
    Setting(
      key: 'plantCategoryManage',
      icon: IconValue.listItemCategory,
      color: ColorValue.appInfoColor,
      page: ''
    ),
    Setting(
      key: 'plantManage',
      icon: IconValue.listItemPlant,
      color: ColorValue.appSuccessColor,
    ),
    Setting(
      key: 'aboutApp',
      icon: IconValue.listItemAbout,
      color: ColorValue.appWarningColor,
    ),
  ];

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  String getListItemLabel(BuildContext context, String key) {
    switch (key) {
      case "gardenName":
        return AppLocalizations.of(context)!.settingListItemGardenName;
      case "plantCategoryManage":
        return AppLocalizations.of(context)!.settingListItemCategory;
      case "plantManage":
        return AppLocalizations.of(context)!.settingListItemPlan;
      case "aboutApp":
        return AppLocalizations.of(context)!.settingListItemAboutApp;
      default:
        return "";
    }
  }

  void toPage(Setting setting){
    if(setting.key!='aboutApp'){
      Navigator.pushNamed(context, setting.page??'/notFound');
    }else{

    }
  }

  Widget buildList(BuildContext context) {
    return Container(
      height: itemList.length * 50 - 1,
      margin: EdgeInsets.only(
        top: 0,
        left: SizeValue.appContentPadding,
        right: SizeValue.appContentPadding,
        bottom: 0,
      ),
      decoration: BoxDecoration(
        borderRadius: BorderRadius.all(Radius.circular(SizeValue.appBorderRadius)),
        boxShadow: [StyleValue.defaultCardBoxShadow],
        color: Colors.white,
      ),
      child: MediaQuery.removePadding(
        removeTop: true,
        context: context,
        child: ListView.builder(
          controller: scrollController,
          itemCount: itemList.length,
          itemBuilder: (context, index) {
            return RippleContainer(
              borderRadius:
                  index == 0
                      ? BorderRadius.only(
                        topLeft: Radius.circular(SizeValue.appBorderRadius),
                        topRight: Radius.circular(SizeValue.appBorderRadius),
                      )
                      : index == itemList.length - 1
                      ? BorderRadius.only(
                        bottomLeft: Radius.circular(SizeValue.appBorderRadius),
                        bottomRight: Radius.circular(SizeValue.appBorderRadius),
                      )
                      : BorderRadius.zero,
              onTap: () {
                toPage(itemList[index]);
              },
              child: Container(
                padding: EdgeInsets.only(
                  top:
                      SizeValue.appContentPadding +
                      SizeValue.appContentPadding / 3,
                  left: SizeValue.appContentPadding,
                  right: SizeValue.appContentPadding,
                ),
                decoration: BoxDecoration(
                  borderRadius:
                      index == 0
                          ? BorderRadius.only(
                            topLeft: Radius.circular(SizeValue.appBorderRadius),
                            topRight: Radius.circular(SizeValue.appBorderRadius),
                          )
                          : index == itemList.length - 1
                          ? BorderRadius.only(
                            bottomLeft: Radius.circular(SizeValue.appBorderRadius),
                            bottomRight: Radius.circular(SizeValue.appBorderRadius),
                          )
                          : BorderRadius.zero,
                ),
                child: Column(
                  children: [
                    Row(
                      children: [
                        SizedBox(
                          width: 36,
                          child: Icon(
                            itemList[index].icon,
                            size: 23,
                            color: itemList[index].color,
                          ),
                        ),
                        Expanded(
                          child: Text(
                            getListItemLabel(context, itemList[index].key),
                            style: StyleValue.settingListItemStyle,
                          ),
                        ),
                        SizedBox(
                          width: 16,
                          child: Icon(
                            IconValue.rightArrow,
                            size: 20,
                            color: Colors.grey,
                          ),
                        ),
                      ],
                    ),
                    Container(
                      margin: EdgeInsets.only(
                        top:
                            SizeValue.appContentPadding +
                            SizeValue.appContentPadding / 3,
                        left: 8,
                        right: 2,
                      ),
                      decoration: BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                            color:
                                index < itemList.length - 1
                                    ? Colors.grey
                                    : Colors.transparent,
                            width: 0.08,
                          ),
                        ),
                      ),
                    ),
                  ],
                ),
              ),
            );
          },
        ),
      ),
    );
  }

  Widget buildExitBtn() {
    return Container(
      width:
          MediaQuery.of(context).size.width - SizeValue.appContentPadding * 2,
      margin: EdgeInsets.only(top: SizeValue.appContentPadding),
      child: OutlinedButton(
        style: OutlinedButton.styleFrom(
          padding: EdgeInsets.symmetric(vertical: 0, horizontal: 0),
          foregroundColor: ColorValue.appPrimaryColor,
          backgroundColor: Colors.white,
          side: BorderSide(color: Colors.white, width: 0.5),
        ),
        onPressed: () {
          AppDialog.showConfirmationDialog(
            context,
            AppLocalizations.of(context)!.confirmationDialogTitle,
            AppLocalizations.of(context)!.settingListItemAboutAppDialogContent,
            () {
              SystemNavigator.pop();
            },
            () {
              Navigator.of(context).pop();
            },
          );
        },
        child: Text( AppLocalizations.of(context)!.exitAppLabel),
      ),
    );
  }

  Widget buildHeaderCard() {
    return AppHeader(
      height: SizeValue.appBarLargeHeight,
      borderRadius: StyleValue.appBarLargeBorderRadius,
      appBar: Row(
        mainAxisAlignment: MainAxisAlignment.spaceBetween,
        children: [
          Text(
            AppLocalizations.of(context)!.settingPageTitle,
            style: StyleValue.appBarTitleStyle,
          ),
        ],
      ),
      child: buildList(context),
    );
  }

  @override
  Widget build(BuildContext context) {
    return AppScaffold(
      body: Column(children: [buildHeaderCard(), buildExitBtn()]),
    );
  }
}
